package com.example.smartwaterapi.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;

import jakarta.annotation.PostConstruct;
import java.util.TimeZone;

/**
 * 时区配置类，确保应用使用东八区（北京/上海）时区
 */
@Configuration
public class TimeZoneConfig {

    /**
     * 应用启动时设置默认时区
     */
    @PostConstruct
    public void init() {
        // 设置JVM默认时区为Asia/Shanghai（东八区）
        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
        System.setProperty("user.timezone", "Asia/Shanghai");
    }
    
    /**
     * 配置Jackson序列化时使用的时区
     */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jacksonObjectMapperCustomization() {
        return jacksonObjectMapperBuilder -> 
            jacksonObjectMapperBuilder.timeZone(TimeZone.getTimeZone("Asia/Shanghai"));
    }
}
